package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.TProductCategory;
import com.ruoyi.business.service.ITProductCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 类目信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-12
 */
@Api(tags = "类目信息管理")
@RestController
@RequestMapping("/business/category")
public class TProductCategoryController extends BaseController
{
    @Autowired
    private ITProductCategoryService tProductCategoryService;

    /**
     * 查询类目信息列表
     */
    @ApiOperation("查询类目列表")
    @PreAuthorize("@ss.hasPermi('business:category:list')")
    @GetMapping("/list")
    public AjaxResult list(TProductCategory tProductCategory)
    {
        List<TProductCategory> list = tProductCategoryService.selectTProductCategoryList(tProductCategory);
        return success(list);
    }




    @ApiOperation("树形结构查询")
    @PreAuthorize("@ss.hasPermi('business:category:tree')")
    @GetMapping("/productCategoryTree")
    public AjaxResult productCategoryTree(TProductCategory tProductCategory)
    {
        List<TProductCategory> list = tProductCategoryService.productCategoryTree(tProductCategory);
        return success(list);
    }





    /**
     * 导出类目信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:category:export')")
    @Log(title = "类目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TProductCategory tProductCategory)
    {
        List<TProductCategory> list = tProductCategoryService.selectTProductCategoryList(tProductCategory);
        ExcelUtil<TProductCategory> util = new ExcelUtil<TProductCategory>(TProductCategory.class);
        util.exportExcel(response, list, "类目信息数据");
    }

    /**
     * 获取类目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(tProductCategoryService.selectTProductCategoryByCategoryId(categoryId));
    }

    /**
     * 新增类目信息
     */
    @ApiOperation("新增类目")
    @PreAuthorize("@ss.hasPermi('business:category:add')")
    @Log(title = "类目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProductCategory tProductCategory)
    {
//        if (!tProductCategoryService.checkDeptNameUnique(tProductCategory))
//        {
//            return error("新增类目'" + tProductCategory.getCategoryName() + "'失败，类目名称已存在");
//        }
//        tProductCategory.setCreateBy(getUsername());
        return toAjax(tProductCategoryService.insertTProductCategory(tProductCategory));
    }

    /**
     * 修改类目信息
     */
    @ApiOperation("修改类目")
    @PreAuthorize("@ss.hasPermi('business:category:edit')")
    @Log(title = "类目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProductCategory tProductCategory)
    {

        Long categoryId = tProductCategory.getCategoryId();
//        tProductCategoryService.checkCategoryDataScope(deptId);
        if (!tProductCategoryService.checkCategoryNameUnique(tProductCategory))
        {
            return error("修改类目'" + tProductCategory.getCategoryName() + "'失败，类目名称已存在");
        }
        else if (tProductCategory.getParentId().equals(categoryId))
        {
            return error("修改部门'" + tProductCategory.getCategoryName() + "'失败，上级类目不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, tProductCategory.getStatus()) && tProductCategoryService.selectNormalChildrenCategoryById(categoryId) > 0)
        {
            return error("该类目包含未停用的子类目！");
        }
        tProductCategory.setUpdateBy(getUsername());
        return toAjax(tProductCategoryService.updateTProductCategory(tProductCategory));
    }

    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long categoryIds) {
//        Long[] ids = Arrays.stream(categoryIds.split(","))
//                .map(Long::valueOf)
//                .toArray(Long[]::new);
        return toAjax(tProductCategoryService.deleteTProductCategoryByCategoryIds(categoryIds));
    }
}
